/**
 * created by zhouli on 2019/1/9
 * 数据权限的id，挂在授权策略对象上，
 * 需要判定数据权限是否开启,只有财务角色才有
 */
import React from 'react';
import {connect} from 'react-redux';
import {Menu, Icon, Modal, Button, Form, Input, Select, message, Alert} from 'antd';
import {deepCopy, messages} from "share/common";
import PDService from 'containers/permission-setting/role-permission-setting/role-permission-detail/components/permission-data/permission-data.service';
import 'styles/permission-setting/role-permission-setting/role-permission-detail/components/permission-data/permission-data.scss';
import configureStore from 'stores';
import {setPermissionData} from 'actions/permission-data';
//规则添加组件
import DataRuleInfo from 'containers/permission-setting/role-permission-setting/role-permission-detail/components/permission-data/data-rule-info';
import {
  conditionExample,
} from 'containers/permission-setting/role-permission-setting/role-permission-detail/components/permission-data/data-rule-model';

const Option = Select.Option;
const SubMenu = Menu.SubMenu;
const Search = Input.Search;
const FormItem = Form.Item;
const confirm = Modal.confirm;

class PermissionData extends React.Component {
  constructor(props) {
    super(props);
    this.state = {}
  }

  componentDidMount() {
    this.getDataPermissionTableItemDetail(this.props.policyData.dataPermissionId);
  }

  //id获取组织权限详情
  getDataPermissionTableItemDetail = (dataPermissionId) => {
    let params = {
      "dataPermissionId": dataPermissionId,
    };
    PDService.getDataPermissionTableItemDetail(params)
      .then((response) => {
        if (response.data && response.data.conditionViewDTOS) {
          configureStore.store.dispatch(setPermissionData(response.data));
        } else {
          configureStore.store.dispatch(setPermissionData({}));
        }
      })
      .catch(err => {
        configureStore.store.dispatch(setPermissionData({}));
      })
  };
  //点击添加一个条件
  actionAddCondition = () => {
    let permissionData = deepCopy(this.props.permissionData);
    if (PDService.checkConditionIsEditing(permissionData)) {
      message.warning(messages('permission-setting.key20')/*正在编辑，请先保存，再添加条件*/);
      return;
    }
    if (!permissionData.conditionViewDTOS) {
      permissionData.conditionViewDTOS = [];
    }
    let conditionViewDTO = deepCopy(conditionExample);
    conditionViewDTO.sequence = permissionData.conditionViewDTOS.length + 1;
    conditionViewDTO.isEditing = true;
    permissionData.conditionViewDTOS.push(conditionViewDTO);
    configureStore.store.dispatch(setPermissionData(permissionData));
  }
  ;
  render() {
    const {permissionData} = this.props;
    return (
      <div className="permission-data-wrap">
        <div className="condition-rule-icon-tips">
          <Alert
            message={messages('permission-setting.key23')/*数据权限是在组织权限的基础上进一步限制数据范围*/}
            description={messages('permission-setting.key24')/*可设置多个条件，单个条件内为且的关系，多个条件间为或的关系*/}
            type="info"
            showIcon
          />
        </div>
        {permissionData.conditionViewDTOS &&
        permissionData.conditionViewDTOS.map((item, index) => {
          return (
            <div
              key={index}
            >
              <DataRuleInfo
                condition={item}
              />
            </div>
          )
        })}
        <div className='add-condition-btn'>
          <Button type='primary' onClick={this.actionAddCondition}> {messages('permission-setting.key25')/*添加条件*/}
          </Button>
        </div>
      </div>
    )
  }
}

PermissionData.propTypes = {
  policyData: React.PropTypes.any,//授权策略对象
};

PermissionData.defaultProps = {
  policyData: {}
};

function mapStateToProps(state) {
  return {
    company: state.login.company,
    tenantMode: state.main.tenantMode,
    permissionData: state.permissionData.permissionData
  }
}

export default connect(mapStateToProps)(PermissionData);
